package com.easonji.core.log.factory;

import java.util.TimerTask;

import com.easonji.admin.mapper.LoginLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.easonji.common.constant.enums.LogSucceedEnum;
import com.easonji.common.constant.enums.LogTypeEnum;
import com.easonji.common.db.Db;
import com.easonji.admin.mapper.OperationLogMapper;
import com.easonji.admin.model.LoginLog;
import com.easonji.admin.model.OperationLog;
import com.easonji.common.util.ToolUtils;

/**
 * @ClassName: LogTaskFactory
 * @Description: 日志操作任务创建工厂
 * @Author: LiuJun
 * @date: 2018/07/08 18:13
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
public class LogTaskFactory {

    private static Logger logger = LoggerFactory.getLogger(LogTaskFactory.class);

    private static LoginLogMapper loginLogMapper = Db.getMapper(LoginLogMapper.class);

    private static OperationLogMapper operationLogMapper = Db.getMapper(OperationLogMapper.class);

    public static TimerTask loginLog(final Integer userId, final String ip) {
        return new TimerTask() {

            @Override
            public void run() {
                try {
                    LoginLog loginLog = LogFactory.createLoginLog(LogTypeEnum.LOGIN, userId, null, ip);
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录日志异常!", e);
                }
            }
        };
    }

    public static TimerTask loginLog(final String username, final String msg, final String ip) {
        return new TimerTask() {

            @Override
            public void run() {
                LoginLog loginLog = LogFactory.createLoginLog(LogTypeEnum.LOGIN_FAIL, null,
                        "账号:" + username + "," + msg, ip);
                try {
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录失败异常!", e);
                }
            }
        };
    }

    public static TimerTask exitLog(final Integer userId, final String ip) {
        return new TimerTask() {

            @Override
            public void run() {
                LoginLog loginLog = LogFactory.createLoginLog(LogTypeEnum.EXIT, userId, null, ip);
                try {
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建退出日志异常!", e);
                }
            }
        };
    }

    public static TimerTask bussinessLog(final Integer userId, final String bussinessName, final String clazzName,
            final String methodName, final String msg) {
        return new TimerTask() {

            @Override
            public void run() {
                OperationLog operationLog = LogFactory.createOperationLog(LogTypeEnum.BUSSINESS, userId, bussinessName,
                        clazzName, methodName, msg, LogSucceedEnum.SUCCESS);
                try {
                    operationLogMapper.insert(operationLog);
                } catch (Exception e) {
                    logger.error("创建业务日志异常!", e);
                }
            }
        };
    }

    public static TimerTask exceptionLog(final Integer userId, final Exception exception) {
        return new TimerTask() {

            @Override
            public void run() {
                String msg = ToolUtils.getExceptionMsg(exception);
                OperationLog operationLog = LogFactory.createOperationLog(LogTypeEnum.EXCEPTION, userId, "", null, null,
                        msg, LogSucceedEnum.FAIL);
                try {
                    operationLogMapper.insert(operationLog);
                } catch (Exception e) {
                    logger.error("创建异常日志异常!", e);
                }
            }
        };
    }
}
